package com.zengde.ai_sandbox.config;

import com.volcengine.ark.runtime.service.ArkService;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class MyArkServiceConfig {

    @Value("${ARK_API_KEY}")
    private String apiKey;

    @Value("${my.doubao.url}")
    private String baseUrl;

    @Bean
    public ArkService arkService() {
        // 显式创建连接池 - 最大空闲连接数50
        ConnectionPool connectionPool = new ConnectionPool(50, 5, TimeUnit.MINUTES);

        // 配置Dispatcher
        Dispatcher dispatcher = new Dispatcher();
        dispatcher.setMaxRequests(200);
        dispatcher.setMaxRequestsPerHost(100);

        return ArkService.builder()
                .apiKey(apiKey)
                .baseUrl(baseUrl) // 设置豆包API地址
                .connectionPool(connectionPool)
                .dispatcher(dispatcher)
                .build();
    }
}